package UI;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import thesis.FSModule;
import thesis.InternTweetInfoExtractor;
import thesis.MoreInfoFrame;
import thesis.Summary;


import com.mongodb.DBObject;

public class VisualizationFrame extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;

	private Summary s;
	private int summaryDimension;
	private int currentPage;
	
	private JButton infoButtons[] = null;
	private JLabel tweetTexts[] = null;
	private JButton next;
	private JButton previous;
	
	public VisualizationFrame(Summary summary){ 
		super("Results"); 
		
		s = summary;
		summaryDimension = FSModule.getSummaryDimension();
		currentPage = 1;
		
		setBounds(0,0, GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width, GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c = this.getContentPane();
		c.setLayout(new GridLayout(3 + summaryDimension, 1));
		
		JPanel panelTitle = new JPanel();
		panelTitle.setLayout(new GridLayout(1,1));
		JLabel title = new JLabel("FILTERING AND SUMMARIZATION MODULE RESULTS");
		Font fontTitle = new Font("Arial", Font.BOLD, 40);
		title.setFont(fontTitle);
		title.setHorizontalAlignment(JLabel.CENTER);
		panelTitle.add(title);
		
		c.add(panelTitle);
		
		JPanel panelStatistics = new JPanel();
		panelStatistics.setLayout(new GridLayout(2,3));
		
		int month = FSModule.getBeginDate().get(GregorianCalendar.MONTH) + 1;
		int day = FSModule.getBeginDate().get(GregorianCalendar.DAY_OF_MONTH);
		int year = FSModule.getBeginDate().get(GregorianCalendar.YEAR);
		int hour = FSModule.getBeginDate().get(GregorianCalendar.HOUR_OF_DAY);
		int minute = FSModule.getBeginDate().get(GregorianCalendar.MINUTE);
		String beginDateString =  "" + month + "-" + day + "-" + year + " " + hour + ":" + minute;
		JLabel beginDate = new JLabel("Begin date: " + beginDateString);
		Font fontStats = new Font("Arial", Font.PLAIN, 25);
		beginDate.setFont(fontStats);
		panelStatistics.add(beginDate);
		
		if(FSModule.getEndDate() != null){
			month = FSModule.getEndDate().get(GregorianCalendar.MONTH) + 1;
			day = FSModule.getEndDate().get(GregorianCalendar.DAY_OF_MONTH);
			year = FSModule.getEndDate().get(GregorianCalendar.YEAR);
			hour = FSModule.getEndDate().get(GregorianCalendar.HOUR_OF_DAY);
			minute = FSModule.getEndDate().get(GregorianCalendar.MINUTE);
			String endDateString =  "" + month + "-" + day + "-" + year + " " + hour + ":" + minute;
			JLabel endDate = new JLabel("End date: " + endDateString);
			endDate.setFont(fontStats);
			panelStatistics.add(endDate);
		}
		else{
			JLabel tweetsNumberLimitLabel = new JLabel("Tweets number limit: " + FSModule.getTweetsNumberLimit());
			tweetsNumberLimitLabel.setFont(fontStats);
			panelStatistics.add(tweetsNumberLimitLabel);
		}
		
		JLabel summaryDim = new JLabel("Summary dimension: " + summaryDimension);
		summaryDim.setFont(fontStats);
		panelStatistics.add(summaryDim);
		
		JLabel quality = new JLabel("Quality: " + s.getQuality());
		quality.setFont(fontStats);
		panelStatistics.add(quality);
		
		JLabel coverage = new JLabel("Coverage: " + s.getCoverage());
		coverage.setFont(fontStats);
		panelStatistics.add(coverage);
		
		JLabel diversity = new JLabel("Diversity: " + s.getDiversity());
		diversity.setFont(fontStats);
		panelStatistics.add(diversity);
		
		c.add(panelStatistics);

		infoButtons = new JButton[summaryDimension];
		tweetTexts = new JLabel[summaryDimension];
		
		for (int i=0; i<summaryDimension; i++){
			JPanel panelTweet = new JPanel();
			panelTweet.setLayout(new GridLayout(2,1));
			
			DBObject tweet = s.getTweets().get(i);
			InternTweetInfoExtractor infoExtr = new InternTweetInfoExtractor(tweet);
			
			tweetTexts[i] = new JLabel(infoExtr.getText());
			Font fontTweets = new Font("Times New Roman", Font.PLAIN, 20);
			tweetTexts[i].setFont(fontTweets);
			
			JPanel panelButton = new JPanel();
			infoButtons[i] = new JButton("more info");
			infoButtons[i].addActionListener(this);
			
			if(i % 2 == 0){
				panelTweet.setBackground(Color.white);
				panelButton.setBackground(Color.white);
			}
			else{
				panelTweet.setBackground(Color.lightGray);
				panelButton.setBackground(Color.lightGray);
			}
			
			
			panelTweet.add(tweetTexts[i]);
			panelButton.add(infoButtons[i]);
			panelTweet.add(panelButton);
			
			c.add(panelTweet);
		}
		
		JPanel panelChangeResults = new JPanel();
		panelChangeResults.setLayout(new GridLayout(2,2));
		panelChangeResults.add(new JPanel());
		panelChangeResults.add(new JPanel());
		
		JPanel panelNext = new JPanel();
		next = new JButton("next");
		next.addActionListener(this);
		if(FSModule.getNumberOfPages() == 1){
			next.setEnabled(false);
		}
		panelNext.add(next);
		
		JPanel panelPrevious = new JPanel();
		previous = new JButton("previous");
		previous.addActionListener(this);
		previous.setEnabled(false);
		panelPrevious.add(previous);
		
		panelChangeResults.add(panelPrevious);
		panelChangeResults.add(panelNext);
		
		c.add(panelChangeResults);
		
	}

	public void actionPerformed(ActionEvent e) {
		for (int i=0; i<summaryDimension; i++){
			if (infoButtons[i] == e.getSource()){
				DBObject tweet = s.getTweets().get(i + (currentPage-1)*summaryDimension);
				
				MoreInfoFrame infoFrame = new MoreInfoFrame(tweet);
				infoFrame.setVisible(true);
			}
		}
		
		if(e.getSource() == next){
			currentPage++;
			previous.setEnabled(true);
			
			if (currentPage == FSModule.getNumberOfPages()){
				next.setEnabled(false);
			}
			
			for (int i=0; i<summaryDimension; i++){
				DBObject tweet = s.getTweets().get(i + (currentPage-1)*summaryDimension);
				InternTweetInfoExtractor infoExtr = new InternTweetInfoExtractor(tweet);
				
				tweetTexts[i].setText(infoExtr.getText());
			}
		}
		
		if(e.getSource() == previous){
			currentPage--;
			next.setEnabled(true);
			
			if (currentPage == 1){
				previous.setEnabled(false);
			}
			
			for (int i=0; i<summaryDimension; i++){
				DBObject tweet = s.getTweets().get(i + (currentPage-1)*summaryDimension);
				InternTweetInfoExtractor infoExtr = new InternTweetInfoExtractor(tweet);
				
				tweetTexts[i].setText(infoExtr.getText());
			}
		}
	}
}
